<?php
/**
 * SBAPIService.php
 * description:
 * Created by www.
 * User: SCF
 * Date: 2018/9/8 15:18
 *
 */

namespace App\Services\ThirdAPI;


use common\utils\RedisKeys;
use App\Models\Order;

class SBAPIService
{
    protected $apiName = 'SB';
    /**
     * 根据版本号取得下500笔投注交易细节
     */
    public function getBetDetail($last_id)
    {
        $data = [
            'api_name' => $this->apiName,
            'last_id' => $last_id
        ];
        $url = config('app.rpcserver') . "/api/getOrders";
        $res = GameApiService::request($url,'get',$data);
        amount_format_array($res['list'],['bet_money','prize_money','winloss_money','contribute_money','bonus_money','valid_money'],true);
        return $res;
    }

    /**
     * 获取队伍名称
     * @param $teamId
     * @return array|string
     */
    public function getTeamName($teamId){
        $data = [
            'api_name' => $this->apiName,
            'team_id' => $teamId
        ];
        $url = config('app.rpcserver') . "/api/getTeamName";
        $res = GameApiService::request($url,'get',$data);
        return $res;

    }

    /**
     * 获取赛事名称
     * @param $teamId
     * @return array|string
     */
    public function getLeagueName($leagueId)
    {
        $data = [
            'api_name' => $this->apiName,
            'league_id' => $leagueId
        ];
        $url = config('app.rpcserver') . "/api/getLeagueName";
        $res = GameApiService::request($url,'get',$data);
        return $res;
    }

    /**
     * 获取投注类型描述
     * @param array $record
     */
    public function getBetType($record=[], $returnArr=false){

        $betType = $record['bet_type'] ?? null;
        $betTeam = $record['bet_team'] ?? null;
        $betTag = $record['bet_tag'] ?? null;
        if(empty($betType) || empty($betTeam)){
            return "";
        }

        $betTypeList = include(__DIR__ . '/sb_bet_type.php');

        $ret1 = $betTypeList[$betType] ?? [];
        if(empty($ret1)){
            return "";
        }

        //获得具体投注内容
        $list = $ret1['list'];
        $betTeamField = "";
        if(!empty($betTag)){
            $betTeamField .= $betTag;
        }
        $betTeamField .= $betTeam;
        $value = $list[$betTeamField] ?? $list[$betTeam] ?? $betTeam;


        if($returnArr){
            return [
                'type' => $ret1['type'],
                'value' => $value,
            ];
        }

        return $ret1['type'] . '.' . $value;
    }

    /**
     * 获取体育种类名
     * @param int $type 体育种类代码
     */
    public function getSportType($type = 0)
    {
        require __DIR__ . '/sb_sports_type.php';

        if (empty($sport_rules)) {
            $sport_name = "未知";
        } else {
            isset($sport_rules[$type]) ? $sport_name = $sport_rules[$type] : $sport_name = "未知";
        }

        return $sport_name;
    }

    /**
     * 时区时间转换
     */
    public function toTimeZone(
        $time_str,
        $format = 'Y-m-d H:i:s',
        $from_tz = 'America/Santo_Domingo',
        $to_tz = 'Asia/Hong_Kong'
    ) {
        $datetime = new \DateTime($time_str, new \DateTimeZone($from_tz));
        $datetime->setTimeZone(new \DateTimeZone($to_tz));
        return $datetime->format($format);
    }

}